package cn.enjoy.utils;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.net.URL;

public class QDHtmlParseUtils {


    public static void main(String[] args) throws IOException {
        String url = "https://search.jd.com/Search?keyword=java";

        Document document = Jsoup.parse(new URL(url),30000);

        Element element = document.getElementById("J_goodsList");
        Elements elements = element.getElementsByTag("li");
//        Element sun1 = elements.get(0);
//        System.out.println(sun1.toString());
//        System.out.println(element.toString());
        int i = 0;
        for (Element sun : elements){
            String img = sun.getElementsByTag("img").eq(0).attr("src");
            String price = sun.getElementsByClass("p-price").eq(0).text();
            String title = sun.getElementsByClass("p-name").eq(0).text();

            System.out.println("==========================");
            System.out.println("img = " + img);
            System.out.println("price = " + price);
            System.out.println("title = " + title);
            i++;
            if (i > 4){
                break;
            }
        }
    }

}
